package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.KeyEvent;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Animation;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class musicStage1_1 extends GLScreen{

	Camera2D guiCam;
	SpriteBatcher batcher;
	
	MusicStageRender musicStageRender;
	MusicStageUpdate musicStageUpdate;
	Bow01 bow01;
	MusicUnit musicUnit;
	
	//북 충돌 영역
	Rectangle buk1, buk2, buk3, buk4, buk5;
	
	Vector2 touchPoint;
	
	float unitX;
	
	public musicStage1_1(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		
		musicStageUpdate = new MusicStageUpdate();
		musicStageRender = new MusicStageRender(batcher, glGraphics, musicStageUpdate);
		musicUnit = new MusicUnit(unitX, 390);
		
		//북 충돌 영역
		buk1 = new Rectangle(30, 70, 74, 100);
		buk2 = new Rectangle(200, 30, 74, 100);
		buk3 = new Rectangle(365, 30, 74, 100);
		buk4 = new Rectangle(532, 30, 74, 100);
		buk5 = new Rectangle(700, 70, 74, 100);
		
		unitX = 30;

		touchPoint = new Vector2();
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		
		List<KeyEvent> keyEvents = game.getInput().getKeyEvents();
		
		int key = keyEvents.size();
		for (int i = 0; i < key; i++) {
			KeyEvent event2 = keyEvents.get(i);
			if(event2.type == KeyEvent.KEY_DOWN) {
				if (event2.keyCode == android.view.KeyEvent.KEYCODE_BACK) {
					((Activity)game).runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							new AlertDialog.Builder((Activity)game).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Quit").setMessage("Do you want to quit").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									game.setScreen(new ActOneScreen(game));
									
								}
							}).setNegativeButton( "No", null ).show(); 
						}
					});
					
					
				}
			}
		}
		
		
		game.getInput().getKeyEvents();
		
		musicStageUpdate.addMusicUnit();

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				touchPoint.set(event.x, event.y);
				guiCam.touchToWorld(touchPoint);
				
				//첫번째 북 클릭시
            	if (OverlapTester.pointInRectangle(buk1, touchPoint)) {
            		musicStageUpdate.update(deltaTime);
            		
            		musicUnit = musicStageUpdate.musicUnits.get(0);
            		musicStageUpdate.addBow(musicUnit.position.x, musicUnit.position.y);
            		
            		int len2 = musicStageUpdate.musicUnits.size();
            		for(int j = 0; j < len2; j++){
            			musicUnit = musicStageUpdate.musicUnits.get(j);
            			musicUnit.position.x += 1;
            		}
            		
             		musicStageRender.renderBow01();
					return;
				}
            	//두번째 북 클릭시
            	if (OverlapTester.pointInRectangle(buk2, touchPoint)) {
            		musicStageUpdate.update(deltaTime);
            		
            		musicUnit = musicStageUpdate.musicUnits.get(1);
            		musicStageUpdate.addBow(musicUnit.position.x, musicUnit.position.y - 40);
            		
            		int len2 = musicStageUpdate.musicUnits.size();
            		for(int j = 0; j < len2; j++){
            			musicUnit = musicStageUpdate.musicUnits.get(j);
            			musicUnit.position.x += 1;
            		}
            		
             		musicStageRender.renderBow01();
					return;
				}
            	//세번째 북 클릭시
            	if (OverlapTester.pointInRectangle(buk3, touchPoint)) {
            		musicStageUpdate.update(deltaTime);
            		
            		musicUnit = musicStageUpdate.musicUnits.get(2);
            		musicStageUpdate.addBow(musicUnit.position.x, musicUnit.position.y - 80);
            		
            		int len2 = musicStageUpdate.musicUnits.size();
            		for(int j = 0; j < len2; j++){
            			musicUnit = musicStageUpdate.musicUnits.get(j);
            			musicUnit.position.x += 1;
            		}
            		
             		musicStageRender.renderBow01();
					return;
				}
            	//네번째 북 클릭시
            	if (OverlapTester.pointInRectangle(buk4, touchPoint)) {
            		musicStageUpdate.update(deltaTime);
            		
            		musicUnit = musicStageUpdate.musicUnits.get(3);
            		musicStageUpdate.addBow(musicUnit.position.x, musicUnit.position.y - 120);
            		
            		int len2 = musicStageUpdate.musicUnits.size();
            		for(int j = 0; j < len2; j++){
            			musicUnit = musicStageUpdate.musicUnits.get(j);
            			musicUnit.position.x += 1;
            		}
            		
             		musicStageRender.renderBow01();
					return;
				}
            	//다섯번째 북 클릭시
            	if (OverlapTester.pointInRectangle(buk5, touchPoint)) {
            		musicStageUpdate.update(deltaTime);
            		
            		musicUnit = musicStageUpdate.musicUnits.get(4);
            		musicStageUpdate.addBow(musicUnit.position.x, musicUnit.position.y - 160);
            		
            		int len2 = musicStageUpdate.musicUnits.size();
            		for(int j = 0; j < len2; j++){
            			musicUnit = musicStageUpdate.musicUnits.get(j);
            			musicUnit.position.x += 1;
            		}
            		
             		musicStageRender.renderBow01();
					return;
				}
            	
            	
            }
        }
		musicStageUpdate.bowDelete();
		musicStageRender.stateTime += deltaTime + 0.1f;
		
	}

	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        musicStageRender.render(deltaTime);
        
		guiCam.setViewportAndMatrices();
		gl.glDisable(GL10.GL_TEXTURE_2D);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}
	
	

}
